Conversation
doitchuu
approved these changes
Mar 30, 2026
doitchuu
reviewed
Mar 30, 2026
| } | ||
|
|
||
| return count; | ||
| }; |
| var isSameTree = function (p, q) { | ||
| if (!p && !q) return true; // 둘 다 없으면(null) | ||
| if (!p || !q) return false; // 둘 중 하나만 없으면(null) | ||
| if (p.val !== q.val) return false; // 값이 다르면 |
O(n)이 아니라, 이거보다 훨씬 빠릅니다 |
raejun92
approved these changes
Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
이렇게 풀었어요
1. Same Tree
1) 복잡도 계산
시간 복잡도: O(n)
공간 복잡도: O(n)
2) 접근 아이디어
단순히 그냥 배열 2개 같은지 비교하면 되는거 아니냐 생각했다. 하지만, 트리는 선형 자료구조가 아니고, "구조 자체가 정보"이다. 문제 예시로 있던 아래 트리만 봐도
그냥 값만 보면 같아보이지만 왼쪽에 있는지 오른쪽에 있는지 구조가 다르므로 false다.
크게 3가지 부분을 비교해야한다.
재귀로 풀어야 할 것 같다.
2. Number of 1 Bits
1) 복잡도 계산
시간 복잡도: O(n)
공간 복잡도: O(1)
2) 접근 아이디어
주어진 정수의 이진수 표현에서 1이 몇 개 있는지 구하자.
저번 시간에 푼 문제에 적용했던 연산
n = n & (n-1)을 쓰면 될 것 같다. 이 연산은 가장 오른쪽에 있는 1비트 하나가 제거되는 방식이다. 했던대로 제거하면서 count++로 증가시키면 그게 결국 1비트의 개수가 된다.